Skip to content

feat(conversation-manager): improve tool result truncation strategy#1756

Merged
mkmeral merged 3 commits intostrands-agents:mainfrom
kevmyung:feat/improve-truncation-strategy
Mar 2, 2026
Merged

feat(conversation-manager): improve tool result truncation strategy#1756
mkmeral merged 3 commits intostrands-agents:mainfrom
kevmyung:feat/improve-truncation-strategy

Conversation

@kevmyung
Copy link
Contributor

Description

Improves the SlidingWindowConversationManager truncation strategy to reduce
context size more gracefully without misleading the agent.

  • Partially truncate large tool results (>400 chars) preserving first/last 200 chars,
    instead of replacing entire content with an error message
  • Replace image blocks (top-level and inside toolResult) with descriptive text placeholders
  • Target oldest tool results first so the most relevant recent results are preserved longer
  • Add ValueError for invalid per_turn values (0 or negative integer)
  • Rename _find_last_message_with_tool_results_find_oldest_message_with_tool_results
    to accurately reflect iteration order

Related Issues

Closes #1545

Documentation PR

N/A

Type of Change

Other: Enhancement - improve existing truncation behavior

Testing

  • I ran hatch run prepare

Added unit tests covering partial truncation, image block replacement,
oldest-first ordering, idempotency guard, and boundary conditions.

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

@kevmyung
Copy link
Contributor Author

@JackYPCOnline Tagging you for review!

@github-actions
Copy link

Assessment: Comment

This PR improves the truncation strategy with a solid implementation that preserves context rather than completely replacing tool results. The test coverage is comprehensive.

Review Summary
  • Configurability: The _PRESERVE_CHARS constant should be exposed as a constructor parameter to align with the "configurable character threshold" mentioned in [FEATURE] Improve tool result truncation strategy for graceful context reduction #1545 and the SDK's extensibility tenet
  • Code Organization: The _truncate_tool_results method handles multiple concerns and could benefit from helper method extraction for maintainability
  • Documentation: Class-level docstring should mention the new truncation behavior for discoverability

Good work on maintaining backward compatibility and implementing oldest-first ordering.

Partially truncate large tool results (preserve first/last 200 chars) instead of replacing with error.
Replace image blocks with placeholders, and target oldest tool results first.
@kevmyung kevmyung force-pushed the feat/improve-truncation-strategy branch from 1942cf8 to fb64029 Compare February 27, 2026 18:53
@github-actions github-actions bot removed the size/m label Feb 27, 2026
@github-actions
Copy link

Assessment: Comment

Thanks for updating the class docstring! The truncation behavior is now well-documented (lines 26-29).

Review Status

Addressed:

  • ✅ Class docstring now documents the truncation behavior

Still Open (from previous review threads):

The implementation is solid and test coverage is comprehensive. The configurability question is worth addressing to fully align with #1545's requirements, but could also be a follow-up PR if preferred.

@codecov
Copy link

codecov bot commented Feb 27, 2026

Codecov Report

❌ Patch coverage is 94.11765% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ion_manager/sliding_window_conversation_manager.py 94.11% 1 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

@mkmeral mkmeral merged commit 1a3b429 into strands-agents:main Mar 2, 2026
19 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Improve tool result truncation strategy for graceful context reduction

3 participants